package com.azt.provider.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.azt.model.page.Pagination;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.azt.api.dto.hot.ProShopFloorExt;
import com.azt.api.pojo.ProShopCompany;
import com.azt.api.pojo.ProShopFloor;
import com.azt.api.pojo.ProShopProduct;
import com.azt.api.service.ProShopFloorService;
import com.azt.provider.mapper.ProShopCompanyMapper;
import com.azt.provider.mapper.ProShopFloorMapper;
import com.azt.provider.mapper.ProShopProductMapper;

@Service
@Transactional
public class ProShopFloorServiceImpl implements ProShopFloorService{
	@Autowired
	private ProShopFloorMapper proShopFloorMapper;
	@Autowired
	private ProShopCompanyMapper proShopCompanyMapper;
	@Autowired
	private ProShopProductMapper proShopProductMapper;
	@Override
	//查询proShopFloor集合
	public List<ProShopFloorExt> getProShopFloorList() {
		List<ProShopFloorExt> getProShopFloorList=proShopFloorMapper.getProShopFloor();
		return getProShopFloorList;
	}
	
	//删除数据
	@Override
	public void deleteById(int floorId) {
		//根据楼层Id删除数据
	  	proShopFloorMapper.deleteByPrimaryKey(floorId);
	  	//根据商铺楼层Id删除数据
	  	ProShopCompany company = new ProShopCompany();
	  	company.setFloorId(floorId);
	  	proShopCompanyMapper.delete(company);
	  	//根据产品楼层Id删除数据
	  	ProShopProduct product = new ProShopProduct();
	  	product.setFloorId(floorId);
	  	proShopProductMapper.delete(product);

	}
    
	//查询楼层数量
	@Override
	public List<Integer> getShopFloor() {
		List<Integer> getshopfloor=proShopFloorMapper.getShopFloor();
		return getshopfloor;
	}

	@Override
	public Pagination findCenProduct(int pageno, int pagesize, Map<String, String> params) {
		Page<Object> startPage = PageHelper.startPage(pageno, pagesize);
		List<Map<String, String>> productList = proShopFloorMapper.findCenProduct(params);
		Pagination p = new Pagination(startPage, productList);
		p.setList(productList);
		p.setParams(params);
		return p;
	}
    
	//新增
	@Override
	public void insert(ProShopFloor proShopFloor) {
		proShopFloorMapper.insertSelective(proShopFloor);
		Integer floorId=proShopFloor.getId();
		List<ProShopCompany> companyList=proShopFloor.getCompanyList();
		for(ProShopCompany company:companyList){
			company.setFloorId(floorId);
			proShopCompanyMapper.insertSelective(company);
		}

		List<ProShopProduct> productList=proShopFloor.getProductList();
		for(ProShopProduct product:productList){
			product.setFloorId(floorId);
			proShopProductMapper.insertSelective(product);
		}
	}
   
	//根据Id查询
	@Override
	public ProShopFloor selectById(Integer id) {
	   ProShopFloor shopFloor=proShopFloorMapper.selectByPrimaryKey(id);
	   ProShopCompany company=new ProShopCompany();
	   company.setFloorId(id);
	   List<ProShopCompany> companyList=proShopCompanyMapper.select(company);
	   ProShopProduct product=new ProShopProduct();
		product.setFloorId(id);
	    List<ProShopProduct> productList=proShopProductMapper.getProShopProduct(id);
	   shopFloor.setCompanyList(companyList);
	   shopFloor.setProductList(productList);
       return shopFloor;
	}
	
	//编辑数据
	@Override
	public void editFloor(ProShopFloor shopFloor){
		proShopFloorMapper.updateByPrimaryKeySelective(shopFloor);
		ProShopProduct shopProduct = new ProShopProduct();
		shopProduct.setFloorId(shopFloor.getId());
		proShopProductMapper.delete(shopProduct);
		List<ProShopProduct> productList=shopFloor.getProductList();
		for(ProShopProduct product:productList){
			product.setFloorId(shopFloor.getId());
			proShopProductMapper.insertSelective(product);
		}
		
		ProShopCompany shopCompany = new ProShopCompany();
		shopCompany.setFloorId(shopFloor.getId());
		proShopCompanyMapper.delete(shopCompany);
		List<ProShopCompany> companyList = shopFloor.getCompanyList();
		for(ProShopCompany company:companyList){
			company.setFloorId(shopFloor.getId());
			proShopCompanyMapper.insertSelective(company);
		}
	}
	
}
	


